/// @tags:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & -x)

using namespace std;

namespace BlueQuantum {

int const N = 1e6 + 5;

int n;
int k[N], hash[N], val[N];

inline void modify(int x, int v) {
  for (int i = x; i <= n; i += lowbit(i)) val[i] = max(val[i], v);
}

inline int query(int x) {
  int res = 0;
  while (x) {
    res = max(res, val[x]);
    x -= lowbit(x);
  }
  return res;
}

inline void Modify(int x, int v) {
  for (int i = x; i; i -= lowbit(i)) val[i] = max(val[i], v);
}

inline int Query(int x) {
  int res = 0;
  while (x <= n) {
    res = max(res, val[x]);
    x += lowbit(x);
  }
  return res;
}

inline int main() {
  cin >> n;
  long long ans = 0;
  for (int i = 1; i <= n; ++i)
    cin >> k[i], hash[i] = k[i], ans = max(ans, (long long)i * k[i]);
  sort(hash + 1, hash + n + 1);
  *hash = unique(hash + 1, hash + n + 1) - hash - 1;
  for (int i = 1; i <= n; ++i) {
    k[i] = lower_bound(hash + 1, hash + *hash + 1, k[i]) - hash;
    ans = max(ans, (long long)(Query(k[i]) + i) * hash[k[i]]);
    Modify(k[i], i);
  }
  memset(val, 0, sizeof(val));
  for (int i = n; i; --i) {
    ans = max(ans, (long long)(Query(k[i]) + i) * hash[k[i]]);
    Modify(k[i], i);
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("「EZEC-5」人赢.in", "r", stdin);
  freopen("「EZEC-5」人赢.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
